<?php

declare(strict_types=1);

namespace newbie\Traits;

use Illuminate\Database\Eloquent\Builder;

trait ModelMacroTrait {
    /**
     * 注册自定义方法
     */
    private function registerUserDataScope () {
        // 数据权限方法
        $model = $this;
        Builder::macro('userDataScope', function (?int $userid = null) use ($model) {
            return $model;
        });
    }

    /**
     * Description:注册常用自定义方法
     * User:mike
     */
    private function registerBase () {
        //添加andFilterWhere()方法
        Builder::macro('andFilterWhere', function ($key, $operator, $value = NULL) {
            if ($value === '' || $value === '%%' || $value === '%') {
                return $this;
            }
            if ($operator === '' || $operator === '%%' || $operator === '%') {
                return $this;
            }
            if ($value === NULL) {
                return $this->where($key, $operator);
            } else {
                return $this->where($key, $operator, $value);
            }
        });

        //添加orFilterWhere()方法
        Builder::macro('orFilterWhere', function ($key, $operator, $value = NULL) {
            if ($value === '' || $value === '%%' || $value === '%') {
                return $this;
            }
            if ($operator === '' || $operator === '%%' || $operator === '%') {
                return $this;
            }
            if ($value === NULL) {
                return $this->orWhere($key, $operator);
            } else {
                return $this->orWhere($key, $operator, $value);
            }
        });
        return $this;
    }
}
